package com.examples.ry.graphics;

import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class ClippingRegion {
	public static void main(String[] args) {
		final Random rand = new Random(System.currentTimeMillis());
		final Display display = new Display();
		final Shell shell = new Shell(display, SWT.SHELL_TRIM
				| SWT.NO_REDRAW_RESIZE);
		shell.addListener(SWT.Paint, new Listener() {
			public void handleEvent(Event event) {
				GC gc = event.gc;
				// problem with color usage here (see text).
				Color color = new Color(display, rand.nextInt(256), rand
						.nextInt(256), rand.nextInt(256));
				gc.setBackground(color);
				gc.fillRectangle(shell.getClientArea());
				color.dispose();
			}
		});
		shell.setText("Clipping Region");
		shell.setSize(250, 150);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
